package cn.itsource.system;

import cn.itsource.admin.Admin;
import cn.itsource.student.Student;

import java.sql.SQLOutput;
import java.util.Arrays;
import java.util.Scanner;

public class SystemTest {
    //新建一个管理员对象
   static Admin[] admins= {new Admin("admin","123456")};

    //新建一个学生表的对象
   static Student[] students=new Student[]{new Student("1001","龚俊",31,"男","1984638488","四川成都"),
                                      new Student("1002","张哲瀚",18,"男","14494947628","江西新余"),
                                      new Student("1003","周子舒",25,"男","1984848848","四季山庄"),
                                      new Student("1004","霍言",28,"男","192828628","四川成都"),
                                        new Student("1005","温客行",29,"男","19383839938","鬼谷"),};

       static Scanner sc=new Scanner(System.in);
    public static boolean login(){
        System.out.println("请输入管理员账号");
        String name=sc.next();
        System.out.println("请输入管理员密码");
        String pwd= sc.next();
        /**
         * 判断管理
         */
            for (Admin admin:admins) {
                if (name.equals(admin.AdName) && pwd.equals(pwd)) {
                    //admin.name是admin类里面的名字和输入的名字做比较，因为在不同的类中，所以admin类中的属性要是public 公共类
                    return true;
                }
            }
        return false;
    }
    //主菜单页面
    public static  void meau(){
        System.out.println("请输入以下编号进行操作");
        System.out.println("1.查询所有学生信息");
        System.out.println("2.查询单个学生信息");
        System.out.println("3.修改学生信息");
        System.out.println("4.增加学生信息");
        System.out.println("5.删除学生信息");
        System.out.println("6.退出系统");

        int num=sc.nextInt();
        switch (num){
            case 1:      System.out.println("------------------");
                         selectStudentAll();
                            System.out.println("------------------");
                         meau();
                         System.out.println("------------------");
                break;

            case 2:     System.out.println("------------------");
                        selectStudentById();
                         System.out.println("------------------");
                        meau();
                        System.out.println("------------------");
                break;
            case 3:     System.out.println("------------------");
                        updateStudentInformation();
                         System.out.println("------------------");
                        meau();
                        System.out.println("------------------");
                        break;
            case 4:   System.out.println("------------------");
                        add();
                System.out.println("------------------");
                        meau();
                System.out.println("------------------");
                        break;
            case 5:   System.out.println("------------------");
                        delete();
                        meau();
                System.out.println("------------------");
            break;
            case 6:  System.out.println("6.退出系统");
            break;

        }

    }
    //查询所有学生操作
    public static void selectStudentAll(){
        System.out.println("学号\t名字\t年龄\t性别\t电话\t\t地址");
        for (Student stu :students) {
            System.out.println(stu.StuId+"\t"+stu.StuName+"\t"+stu.age+"\t"+stu.Sex+"\t"+stu.Phone+"\t\t"+stu.Address);

        }

    }
    //根据id查询单条学生
    public static void selectStudentById(){
        String id= sc.next();
        for (Student stu:students) {
                if(id.equals(stu.StuId))
                {
                    System.out.println(stu.StuId+"\t"+stu.StuName+"\t"+stu.age+"\t"+stu.Sex+"\t"+stu.Phone+"\t\t"+stu.Address);
                }
        }

    }
    //修改学生信息
    public static  void updateStudentInformation(){
        System.out.println("请输入一个要修改的学号");
        String id= sc.next();
        for (Student stu:students) {
            if(id.equals(stu.StuId))
            {
                System.out.println("请输入名字");
                stu.StuName=sc.next();
                System.out.println("请输入年龄");
                stu.age=sc.nextInt();
                System.out.println("请输入性别");
                stu.Sex=sc.next();
                System.out.println("请输入电话");
                stu.Phone=sc.next();
                System.out.println("请输入地址");
                stu.Address=sc.next();
                System.out.println("修改数据成功");
                return;
            }
        }
        System.out.println("这个学生不存在，请重新输入学号");

    }

    /**
     * 		/*
     * 			 * 证明不存在这个学号，可以输入到系统中
     * 			 * 1、使用数组工具类的复制方法（copyOf），将原数组的学生信息复制到新数组中，新数组的长度是原数组+1
     * 			 * 2、还是使用原数组的数组名来接收新数组的地址值，保证后续操作一直都是最新的数组
     * 			 * 3、创建一个学生对象，给对象中的属性赋值
     * 			 * 4、然后将最新的学生对象放入到数组中的最后一位
     */
    public  static void add(){
        System.out.println("输入一个要添加的学号");
        String Id=sc.next();
        boolean flag=false;
        for (Student stu:students) {
            if (Id.equals(stu.StuId)) {
                flag=true;
                break;
            }
        }
        if(flag){
            System.out.println("这个学号已经存在，请重新输入一个学号");
        }
        else
        {
            students= Arrays.copyOf(students,students.length+1);
            Student stu=new Student();
            stu.StuId=Id;
            System.out.println("输入姓名");
            stu.StuName=sc.next();
            System.out.println("输入年龄");
            stu.age=sc.nextInt();
            System.out.println("输入性别");
            stu.Sex=sc.next();
            System.out.println("输入电话");
            stu.Phone=sc.next();
            System.out.println("输入地址");
            stu.Address=sc.next();
            students[students.length-1]=stu;
        }
    }
    public static void delete(){
        System.out.println("输入要删除的记录");
        String Id=sc.next();
        int index=0;
        for (Student stu:students) {
            if (Id.equals(stu.StuId)) {
                Student[] newStu = new Student[students.length - 1];
                for (int i = 0; i <students.length ; i++) {
                    if (students[i].StuId.equals(stu.StuId)){
                        continue;
                    }
                        newStu[index++]=students[i];
                }
                students=newStu;
            }
        }

    }
    public static void main(String[] args) {
          boolean flag=login();
          if (flag)
          {
              System.out.println("欢迎登录学生管理系统");
              meau();
          }
          else
          {
              System.out.println("密码或用户名出错");
          }

    }
}
